﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Zor_DAL;
using Zor_Modes;
using Zor_Tools;

namespace Zor_Bll
{
    public class Zor_Invoice_Bll<T> where T : class
    {
        /// <summary>
        /// 获取管理列表
        /// </summary>
        /// <param name="index">页码</param>
        /// <param name="TotalCount">返回值</param>
        /// <returns></returns>
        public static List<T> GetPageList(dynamic Model, int index, out int TotalCount, string where)
        {
            int Dtid = RequestSession.GetSessionUser().DT_Id; //获取当前登录人的部门ID
            if (Model.Tenant_Name == null &&
                Model.Lease_StartTime == null &&
                Model.NatPer_Name == null &&
                Model.NatPer_Phone == null &&
                Model.NatPer_IDNumber == null &&
                Model.Acceptance_TicketNumber == null &&
                Model.Acceptance_TaxNumber == null &&
                Model.InState == null &&
                Model.AcceptanceType == null
                )
            {
                var sqlSurch = " Dt_Id  in (select Dt_Id  from dbo.Sys_Department where Dt_ParentId=" + Dtid + ") and EReviewRent <=3150000 and (InState = '未开发票' or InState='已开发票') ";
                return PagerHelp<T>.GetPagerList(index, "V_Zor_Invoice", "In_CreateDate desc", out TotalCount, sqlSurch);
            }
            return PagerHelp<T>.GetPagerList(index, "V_Zor_Invoice", "In_CreateDate desc ", out TotalCount, GetSearchWhere(Model, "Dt_Id  in (select Dt_Id  from dbo.Sys_Department where Dt_ParentId=" + Dtid + ") and EReviewRent <=3150000  "));
        }
     
        /// <summary>
        /// 录入发票号
        /// </summary>
        /// <param name="accpid">受理单ｉｄ</param>      
        /// <param name="In_Id">发票号主键ｉｄ</param>
        /// <param name="pt_Id">税收ｉｄ</param>
        /// <param name="Grsds">个人所得税</param>
        /// <param name="Fcs">房产税</param>
        /// <param name="acttualtax">税金</param>
        /// <returns></returns>
        public static BaseAnswer UpdateInvoice(double Grsds, double Fcs, int In_Id, int accpid, int pt_Id, string InNumBer, double acttualtax)
        {
            BaseAnswer bar = Zor_Invoice_DAL<T>.UpdateInvoice(Grsds, Fcs,In_Id, accpid, pt_Id, InNumBer, acttualtax);
            if (bar.AnswerType != BaseAnswer.Answers.Error)
                bar.AnswerMeg = bar.AnswerType == BaseAnswer.Answers.Succee ? "开票成功！" : "开票失败！";
            return bar;    
        }
        /// <summary>
        /// 修改录入发票号
        /// </summary>
        /// <param name="accpid">受理单ｉｄ</param>      
        /// <param name="In_Id">发票号主键ｉｄ</param>
        /// <param name="pt_Id">税收ｉｄ</param>
        /// <returns></returns>
        public static BaseAnswer UpdateInvoiceNum(int In_Id, string InNumBer)
        {
            BaseAnswer bar = Zor_Invoice_DAL<T>.UpdateInvoiceNum(In_Id, InNumBer);
            if (bar.AnswerType != BaseAnswer.Answers.Error)
                bar.AnswerMeg = bar.AnswerType == BaseAnswer.Answers.Succee ? "修改成功！" : "修改失败！";
            return bar;
        }
        #region 私有函数

        private static string GetSearchWhere(dynamic Model, string where)
        {
            var sqlSurch = string.Empty;
            if (Model.Tenant_Name != null)
            {
                sqlSurch += " and Tenant_Name like '%" + Model.Tenant_Name + "%'";
            }
            if (Model.Lease_StartTime != null && Model.Lease_EndTime != null)
            {
                sqlSurch += " and In_Date >=" + TimeHelper.ToTimeStamp(DateTime.Parse(Model.In_Date.ToString())) + " and In_Date<=" + TimeHelper.ToTimeStamp(DateTime.Parse(Model.In_Date.ToString())) + "";
            }
            if (Model.NatPer_Name != null)
            {
                sqlSurch += " and NatPer_Name like '%" + Model.NatPer_Name + "%'";
            }
            if (Model.NatPer_Phone != null)
            {
                sqlSurch += " and NatPer_Phone = '" + Model.istaxregistration + "'";
            }
            if (Model.NatPer_IDNumber != null)
            {
                sqlSurch += " and NatPer_IDNumber = '" + Model.NatPer_IDNumber + "'";
            }
            if (Model.Acceptance_TaxNumber != null)
            {
                sqlSurch += " and Acceptance_TaxNumber = '" + Model.Acceptance_TaxNumber + "'";
            }
             if (Model.Acceptance_TicketNumber != null)
            {
                sqlSurch += " and Acceptance_TicketNumber = '" + Model.Acceptance_TicketNumber + "'";
            }
             if (Model.InState != null)
            {
                if (Model.InState == "全部")
                {
                    sqlSurch += " and InState in ('未开发票','已开发票','已打印发票')";
                }
                else
                {
                    sqlSurch += " and InState = '" + Model.InState + "'";
                }
            }
            if (Model.AcceptanceType != null)
            {
                if (Model.AcceptanceType == "全部")
                {
                    sqlSurch += " and AcceptanceType in('社区','自然人')";
                }
                else
                {
                    sqlSurch += " and AcceptanceType = '" + Model.AcceptanceType + "' ";
                }
            }
            return where + sqlSurch;
        }
        #endregion
    }
}
